//
//  NSDate+ZLCategory.m
//  ZLCategory
//
//  Created by zhaozl on 2022/3/7.
//

#import "NSDate+ZLCategory.h"
#import "NSDateFormatter+ZLCategory.h"

@implementation NSDate (ZLCategory)

+ (NSDate *)zl_dateWithString:(NSString *)dateString format:(NSString *)format {
    NSDateFormatter *formatter = [NSDateFormatter zl_dateFormatterWithFormat:format];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    return [formatter dateFromString:dateString];
}

- (NSInteger)zl_weekdayString {
    NSArray *weekdays = [NSArray arrayWithObjects:[NSNull null], @"7", @"1", @"2", @"3", @"4", @"5", @"6", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    [calendar setTimeZone:timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:self];
    return [[weekdays objectAtIndex:theComponents.weekday] integerValue];
}

- (NSString *)zl_stringWithFormat:(NSString *)format {
    NSDateFormatter *formatter = [NSDateFormatter zl_dateFormatterWithFormat:format];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    return [formatter stringFromDate:self];
}

- (NSString *)zl_stringYYYMMDD {
    return [self zl_stringWithFormat:@"YYYY-MM-dd"];
}

/// 时间偏移
/// @param second 秒
- (NSDate *)zl_offsetSecond:(int)second {
    NSCalendar *calendar = [self defaultCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    components.second = second;
    return [calendar dateByAddingComponents:components toDate:self options:0];
}

/// 时间偏移
/// @param minute 分
- (NSDate *)zl_offsetMinute:(int)minute {
    return [self zl_offsetSecond:minute * 60];
}

/// 时间偏移
/// @param hour 时
- (NSDate *)zl_offsetHour:(int)hour {
    return [self zl_offsetMinute:hour * 60];
}

/// 时间偏移
/// @param day 天
- (NSDate *)zl_offsetDay:(int)day {
    return [self zl_offsetHour:day * 24];
}

/// 年
- (NSInteger)zl_year {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitYear fromDate:self];
    return [components year];
}

/// 月
- (NSInteger)zl_month {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitMonth fromDate:self];
    return [components month];
}

/// 周
- (NSInteger)zl_week {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:self];
    return [weekdayComponents weekday];
}

/// 日
- (NSInteger)zl_day {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitDay fromDate:self];
    return [components day];
}

/// 时
- (NSInteger)zl_hour {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitHour fromDate:self];
    return [components hour];
}

/// 分
- (NSInteger)zl_minute {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitMinute fromDate:self];
    return [components minute];
}

/// 秒
- (NSInteger)zl_second {
    NSCalendar *gregorian = [self defaultCalendar];
    NSDateComponents *components = [gregorian components:NSCalendarUnitSecond fromDate:self];
    return [components second];
}

- (NSCalendar *)defaultCalendar {
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [greCalendar setTimeZone: timeZone];
    return greCalendar;
}

- (long)zl_timestamp {
    return [self timeIntervalSince1970];
}

- (NSInteger)zl_differYears:(NSDate *)date {
    return [[self defaultCalendar] components:NSCalendarUnitYear fromDate:self toDate:date options:0].year;
}

- (NSInteger)zl_differMonths:(NSDate *)date {
    return [[self defaultCalendar] components:NSCalendarUnitMonth fromDate:self toDate:date options:0].month;
}

- (NSInteger)zl_differDays:(NSDate *)date {
    return [[self defaultCalendar] components:NSCalendarUnitDay fromDate:self toDate:date options:0].day;
}

- (NSInteger)zl_differHours:(NSDate *)date {
    return [[self defaultCalendar] components:NSCalendarUnitHour fromDate:self toDate:date options:0].hour;
}

- (NSInteger)zl_differMinutes:(NSDate *)date {
    return [[self defaultCalendar] components:NSCalendarUnitMinute fromDate:self toDate:date options:0].minute;
}

- (NSInteger)zl_differSeconds:(NSDate *)date {
    return [[self defaultCalendar] components:NSCalendarUnitSecond fromDate:self toDate:date options:0].second;
}

- (NSDateComponents *)zl_differ:(NSDate *)date unit:(NSCalendarUnit)unit {
    return [[self defaultCalendar] components:unit fromDate:self toDate:date options:0];
}

- (long)zl_timestamp:(NSInteger)hour andMinute:(NSInteger)minute {
    NSCalendar *calendar = [self defaultCalendar];
    NSDateComponents *dateComponents = [[self defaultCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay  fromDate:self];
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setDay:dateComponents.day];
    [dateComponentsForDate setMonth:dateComponents.month];
    [dateComponentsForDate setYear:dateComponents.year];
    [dateComponentsForDate setHour:hour];
    [dateComponentsForDate setMinute:minute];

    NSDate *dateFromDateComponentsForDate = [calendar dateFromComponents:dateComponentsForDate];
    return [dateFromDateComponentsForDate timeIntervalSince1970];
}

@end
